<template>
  <div class="pakpay">
    <div class="pakpay-form">
      <el-form
        label-position="right"
        label-width="10px"
        style="display: inline-block; display: flex"
      >
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item>
              <el-input
                placeholder="请输入发布小区"
                v-model="form.communityname"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-input
                placeholder="请输入公告主题"
                v-model="form.title"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-input
                placeholder="请输入发布内容"
                v-model="form.content"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-button
          type="success"
          style="margin-left: 20px; width: 9%"
          @click="addreset()"
          ><i class="el-icon-plus"></i>发布公告</el-button
        >

        <el-button type="primary" style="width: 9%" @click="resetForm()"
          ><i class="el-icon-refresh-right"></i>重置</el-button
        >
      </el-form>
    </div>
    <div class="pakpay-table">
      <el-tabs type="border-card">
        <el-tab-pane label="用户管理">
          <el-table :data="tableData" border style="width: 100%">
            <!-- 序号 -->
            <el-table-column type="index" label="序号" width="50">
            </el-table-column>

            <el-table-column prop="title" label="公告主题" width="220">
            </el-table-column>
            <el-table-column prop="content" label="公告内容" width="500">
            </el-table-column>
            <el-table-column prop="createtime" label="发布时间" width="260">
            </el-table-column>
            <el-table-column prop="communityname" label="小区名称" width="170">
            </el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  @click="handleClick(scope.row)"
                  type="danger"
                  size="small"
                  ><i class="el-icon-delete"></i>归档</el-button
                >
                <el-button
                  size="small"
                  type="primary"
                  @click="handleDetail(scope.row)"
                  ><i class="el-icon-zoom-in"></i>查看公告</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 通知缴费详情 -->
    <el-dialog
      title="车位订单详情"
      :visible.sync="dialogVisible1"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="handselect"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区名称" prop="communityname">
              <el-input readonly v-model="handselect.communityname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布时间">
              <el-date-picker
                v-model="handselect.createtime"
                readonly
                type="date"
                placeholder="发布时间"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="公告主题" prop="title">
          <el-input readonly v-model="handselect.title"></el-input>
        </el-form-item>
        <el-form-item prop="orderstatus" label="公告内容" width="200">
          <el-input readonly v-model="handselect.content"> </el-input>
        </el-form-item>

        <el-form-item label="公告状态" disabled prop="parkpayNotify">
          <el-button disabled>阅读量</el-button>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">关闭</el-button>
      </span>
    </el-dialog>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: true,
      total: 0,
      form: {
        title: "",
        content: "",
        communityname: "",
        pageNum: 10,
        currPage: 0,
      },
      dialogVisible1: false,
      tableData: [],
      handselect: [],
      tableData1: "",
    };
  },
  methods: {
    // 添加公告
    async addreset() {
      try {
        const { data: res } = await this.axios.post(
          `poster/addPoster?communityname=${this.form.communityname}&title=${
            this.form.title
          }&content=${this.form.content}&token=${sessionStorage.getItem(
            "token"
          )}`,
          {
            communityname: this.form.communityname,
            title: this.form.title,
            content: this.form.content,
            token: sessionStorage.getItem("token"),
          }
        );
        this.form.title = "";
        this.form.communityname="";
        this.form.content = "";
        if (res == "发布成功") {
          this.$message.success("添加成功");
          this.getTableData();
        } else {
          this.$message.error("添加失败");
        }
      } catch (error) {
        this.handleError("添加失败", error);
      }
    },
    handleSizeChange(val) {
      this.form.pageNum = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.form.currPage = val - 1;
      this.getTableData();
    },
    handleDetail(val) {
      this.dialogVisible1 = true;
      this.handselect = val;
      console.log(val);
    },
    reset() {
      (this.tableData1.startdate = ""),
        (this.tableData1.enddate = ""),
        (this.tableData1.note = ""),
        (this.tableData1.orderstatus = ""),
        (this.tableData1.orderpay = ""),
        this.getTableData();
    },
    handleClick(val) {
      console.log(val);

      this.$confirm("确定永久归档这条公告?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            const response = await this.axios.get(
              `poster/delPoster?p_id=${val.p_id}&token=${sessionStorage.token}`
            ); // 使用val.id作为参数p_
            this.$message({
              type: "success",
              message: "归档公告成功!",
            });
            this.getTableData();
            console.log(response.data); // 输出删除接口的响应
          } catch (error) {
            this.$message({
              type: "error",
              message: "归档公告失败!",
            });
            console.error(error);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消归档公告",
          });
        });
    },
    // 通知缴费弹窗
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done(); // 确认时执行的操作
        })
        .catch(() => {
          done(); // 取消时执行的操作
        });
    },
    async getTableData() {
      try {
        const { data: res } = await this.axios.get("poster/getPosterData?", {
          params: this.form,
        });
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      } catch (error) {
        this.handleError("获取数据失败", error);
      }
    },
    resetForm() {
      this.form = {
        parkname: "",
        orderstatus: "",
        orderpay: "",
        pageNum: 10,
        currPage: 0,
      };
    },
  },
  created() {
    this.getTableData();
  },
};
</script>

<style lang="scss" scoped>
.block {
  text-align: right;
  margin-bottom: 20px;
}
.pakpay {
  margin: 1.3% 1%;
  padding: 0 0;
  background-color: #fff;
  height: calc(100% - 46px);
  display: flex;
  flex-direction: column;

  .pakpay-form {
    padding: 20px;

    .el-form-item {
      margin-bottom: 0; // 调整表单项之间的间距
    }
  }

  .pakpay-table {
    flex: 2;
    padding: 20px;
  }
}
.el-col-8 {
  width: 335px;
}
::v-deep .el-table td.el-table__cell,
.el-table th.el-table__cell.is-leaf {
  border-bottom: 1px solid #ebeef5;
  text-align: center;
}
::v-deep th.el-table__cell.is-leaf {
  border-bottom: 1px solid #ebeef5;
  text-align: center;
}
::v-deep .el-table .el-table__cell {
  padding: 5px 0;
}
</style>